package com.cloud.school.bus.teacherhelper.modules.notice.adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Notice;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.modules.home.BigPictureActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NoticeDetailAdapter extends BaseAdapter {

	private static final String HEAD = "head";
	private static final String STATUS = "status";
	private static final String ITEM = "item";

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private Notice mNotice;

	public NoticeDetailAdapter(Context context, Notice notice) {
		mContext = context;
		mNotice = notice;
		mApplication = (TeacherHelperApplication) context
				.getApplicationContext();
	}

	@Override
	public int getCount() {
		return mNotice.slistname.length+2;
	}

	@Override
	public Object getItem(int arg0) {
		if(0 == arg0){
			return "	";
		}else if (1 == arg0) {
			return "";
		}
		return mNotice.slistname[arg0-2];
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			if (0 == arg0) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_head, null);
				arg1.setTag(HEAD);
			} else if (1 == arg0) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_status, null);
				arg1.setTag(STATUS);
			} else if (arg0 > 1) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_item, null);
				arg1.setTag(ITEM);
			}
		} else {
			if(0 == arg0 && !HEAD.equals(arg1.getTag())){
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_head, null);
				arg1.setTag(HEAD);
			}else if (1 == arg0 && !STATUS.equals(arg1.getTag())) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_status, null);
				arg1.setTag(STATUS);
			}else if (arg0 > 1 && !ITEM.equals(arg1.getTag())) {
				arg1 = LayoutInflater.from(mContext).inflate(
						R.layout.adapter_notice_detail_item, null);
				arg1.setTag(ITEM);
			}
		}
		
		
		if(0 == arg0){
			TextView titleTextView = (TextView)arg1.findViewById(R.id.titleTextView);
			if(TextUtils.isEmpty(mNotice.noticetitle)){
				titleTextView.setVisibility(View.GONE);
			}else {
				titleTextView.setVisibility(View.VISIBLE);
				titleTextView.setText(mNotice.noticetitle);
			}
			TextView contentTextView = (TextView)arg1.findViewById(R.id.contentTextView);
			if (TextUtils.isEmpty(mNotice.noticecontent)) {
				contentTextView.setVisibility(View.GONE);
			} else {
				contentTextView.setVisibility(View.VISIBLE);
				contentTextView.setText(mNotice.noticecontent);
			}
			ImageView imageView = (ImageView)arg1.findViewById(R.id.imageView);
			if (null == mNotice.plist || 0 == mNotice.plist.size()) {
				imageView.setVisibility(View.GONE);
			} else {
				imageView.setVisibility(View.VISIBLE);
				ImageLoader.getInstance().displayImage(mNotice.plist.get(0).source,
						imageView, mApplication.mCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {
								ImageView tempImageView = (ImageView) view;
//								tempImageView.setImageBitmap(loadedImage);
								if(imageUri.startsWith("http://")){
									tempImageView.setImageBitmap(loadedImage);
								}else if(imageUri.startsWith("file:///")){
									ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, tempImageView);
								}else {
									ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, tempImageView);
								}
							}
						});
			}
			imageView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					List<Picture> pictureList = new ArrayList<Picture>();
					Picture picture = new Picture();
					picture.setPicturePath(mNotice.plist.get(0).source);
					pictureList.add(picture);
					Intent intent = new Intent(mContext, BigPictureActivity.class);
					intent.putExtra("pictureList", (ArrayList<Picture>)pictureList);
					mContext.startActivity(intent);
				}
			});
			TextView dateTextView = (TextView)arg1.findViewById(R.id.dateTextView);
			long addtime = 0L;
			try {
				addtime = Long.parseLong(mNotice.addtime);
			} catch (Exception e) {
			}
			if (0 == addtime) {
				dateTextView.setVisibility(View.INVISIBLE);
			} else {
				dateTextView.setVisibility(View.VISIBLE);
				dateTextView.setText(DateUtils.dateFormat(addtime * 1000,
						"MM-dd HH:ss"));
			}
		}else if (1 == arg0) {
			TextView statusLeftTextView = (TextView)arg1.findViewById(R.id.statusLeftTextView);
			TextView statusRightTextView = (TextView)arg1.findViewById(R.id.statusRightTextView);
			if ("0".equals(mNotice.isconfirm) || "".equals(mNotice.isconfirm)) {
				// 不需要回执,显示信封icon
				statusRightTextView.setVisibility(View.GONE);
			} else if ("1".equals(mNotice.isconfirm)) {
				// 需要回执，显示星星icon
				statusRightTextView.setVisibility(View.VISIBLE);
			}
		}else if (arg0 > 1) {
			TextView nameTextView = (TextView)arg1.findViewById(R.id.nameTextView);
			nameTextView.setText(mNotice.slistname[arg0-2]);
			ImageView rightImageView = (ImageView)arg1.findViewById(R.id.rightImageView);
			if("1".equals(mNotice.sisconfirmFlag[arg0-2])){
				rightImageView.setVisibility(View.VISIBLE);
			}else {
				rightImageView.setVisibility(View.GONE);
			}
		}

		return arg1;
	}

}
